//
//  BCHttpRequestModel.m
//  BCBaseNetworking
//
//  Created by Neil 叶青 on 2/24/12.
//  Copyright (c) 2012 Funcity. All rights reserved.
//  网络请求模型


#import "BCHttpRequestModel.h"

@implementation BCHttpRequestModel
#pragma mark - system
- (instancetype) initRequestWithDomain:(NSString *)domain withParam:(NSDictionary<NSString*, id> *_Nullable)param withMethod:(BCBaseRequestMethod)method withSuccess:(void (^)(BCBaseRequestOperation *, BCHttpResponseModel *))successAction withError:(void (^)(BCBaseRequestOperation *, BCHttpResponseModel *))errorAction withTarget:(id)target {
    self = [super init];
    if(self){
        _addUserCookies = YES;
        _needUserAuth = NO;
        _actionRequestSuccess = successAction;
        _actionRequestError = errorAction;
        _targetDelegate = target;
        _requestMethod = method;
        _retryCountOnError = 1;
        _retryCount = 1;
        _timeoutInterval = 0;
        _customPost = param;
    }
    return self;
}
-(void)dealloc
{
    _finalURL=nil;
    _targetDelegate=nil;
    _actionRequestError=nil;
    _actionRequestSuccess=nil;
}

#pragma mark - getter
-(NSMutableDictionary *)serverPostParams
{
    if (!_serverPostParams) {
        _serverPostParams = [[NSMutableDictionary alloc] init];
    }
    return _serverPostParams;
}
-(NSMutableDictionary *)headerParams {
    if (!_headerParams) {
        _headerParams = [[NSMutableDictionary alloc] init];
    }
    return _headerParams;
}
-(NSMutableDictionary *)cookieParams {
    if (!_cookieParams) {
        _cookieParams = [[NSMutableDictionary alloc] init];
    }
    return _cookieParams;
}

#pragma mark - response 回调处理
/**
 *  请求成功回调，子类可以重写
 *
 *  @param request  请求operation
 *  @param response 返回数据对象
 */
-(void)onReponseSuccessAction:(BCBaseRequestOperation *)request withResponse:(BCHttpResponseModel *)response
{
    if(_actionRequestSuccess){
        _actionRequestSuccess(request,response);
    }
}
/**
 *  请求失败回调，子类可以重写
 *
 *  @param request  请求operation
 *  @param response 返回数据对象
 */
-(void)onReponseErrorAction:(BCBaseRequestOperation *)request withResponse:(BCHttpResponseModel *)response
{
    if(_actionRequestError){
        _actionRequestError(request,response);
    }
}
@end
